﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

using Common.Library;
using OnlineVideos.Data;
using Common.Utilities;
using OnlineVideos.UI;
using OnlineVideos.Entities;
using System.Threading.Tasks;
using Windows.Storage;

namespace OnlineVideos
{
    public partial class RenameTitle : PhoneApplicationPage
    {
        public RenameTitle()
        {
            InitializeComponent();
            Loaded += RenameTitle_Loaded;
        }

        void RenameTitle_Loaded(object sender, RoutedEventArgs e)
        {
            Maingrid.Visibility = Visibility.Visible;
            if (Constants.frompagename == "DownloadList")
            {
                tblTitle.Text = "Rename Title";
            }
            else
            {
                tblTitle.Text = "Rename Link";
            }
        }

        private void imgclose_Tapped(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Maingrid.Visibility = Visibility.Collapsed;
        }

        private void SaveForCutVideo_Tapped_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                Maingrid.Visibility = Visibility.Collapsed;
                RenameLinkTitleTable linktable = new RenameLinkTitleTable();
                if (Constants.frompagename == "DownloadList")
                {
                    int showid=Constants.RenameLinkShowlistItem.ShowID;
                    ShowList showlist=Task.Run(async () => await Constants.connection.Table<ShowList>().Where(i => i.ShowID == showid).FirstOrDefaultAsync()).Result;
                    string oldName = showlist.Title;
                    string oldthumb = showlist.TileImage;
                    string genre = showlist.Genre;
                    string oldtitle=string.Empty;
                    if(showlist!=null)
                        oldtitle = showlist.Title;
                    string ext = System.IO.Path.GetExtension(oldtitle);
                    if (!string.IsNullOrEmpty(tb.Text))
                    {

                        showlist.Title = tb.Text+ext;
                        showlist.TileImage = tb.Text + ".jpg";
                    }
                   Constants.connection.UpdateAsync(showlist);
                   ShowLinks showlinks = Task.Run(async () => await Constants.connection.Table<ShowLinks>().Where(i => i.ShowID == showid).FirstOrDefaultAsync()).Result;
                   string oldlinktitle = string.Empty;
                   string oldlinkurl = string.Empty;
                   if (showlinks != null)
                   {
                       oldlinktitle = showlinks.Title;
                       oldlinkurl = showlinks.LinkUrl;
                   }
                   if (!string.IsNullOrEmpty(tb.Text))
                   {
                       showlinks.Title = tb.Text + ext;
                       showlinks.LinkUrl = tb.Text + ext;
                   }
                   Task.Run (async () => await Constants.connection.UpdateAsync(showlinks));
                    //ShowList data=Task.Run(async () => await Constants.connection.Table<ShowList>().Where(i => i.ShowID == showid).FirstOrDefaultAsync()).Result;
                  
                   string newName = tb.Text + ext;
                 
                   if (genre != "Audio")
                   {

                       var file = Task.Run(async () => await ApplicationData.Current.LocalFolder.GetFileAsync(ResourceHelper.ProjectName + "\\" + genre + "\\" + oldName)).Result;
                       Task.Run(async () => await file.RenameAsync(newName));
                   }
                   if (genre == "Videos")
                   {
                       string newthumb = tb.Text + ".jpg";
                       var fileforthumbnailimage = Task.Run(async () => await ApplicationData.Current.LocalFolder.GetFileAsync("Images\\" + oldthumb)).Result;
                       Task.Run(async () => await fileforthumbnailimage.RenameAsync(newthumb));
                   }
                    DownloadList.current.GetPageDataInBackground();
                   
                }
                else
                {
                    linktable.LinkTitle = Constants.RenameLinkItem.title;
                    linktable.LinkUrl = Constants.RenameLinkItem.Downloaduri.ToString();
                    linktable.RenameLinkTitle = tb.Text;

                    Constants.connection.InsertAsync(linktable);
                      PageHelper.NavigateTo(AppResources.Downloadpivot, "3");
                }
               
              
               // PageHelper.NavigateTo(NavigationHelper.Downloadpivot);

                //NavigationService.Navigate(new Uri("/Views/DownloadPivots.xaml", UriKind.Relative));

                
             
            }
            catch (Exception ex)
            {
                string s = ex.Message;
               
            }
        }
    }
}